import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;

import org.junit.Test;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import com.shareach.dao.redis.impl.MatchRDao;
import com.shareach.domain.BaseBankDomain;
import com.shareach.redis.RedisService;
import com.shareach.service.BaseBankCardService;
import com.shareach.service.BaseBankService;

@ContextConfiguration(locations = { "classpath:**/applicationContext.xml" })
public class MainTest extends AbstractJUnit4SpringContextTests {
	@Resource
	private BaseBankCardService baseBankCardService;
	@Resource
	protected MongoTemplate mongoTemplate;
	@Resource
	private RedisService redisService;
	@Resource
	private MatchRDao matchRDao;
	@Resource
	private BaseBankService baseBankService;

	/**
	 * 可选择的删除操作
	 */
	@Test
	public void initData() {
		// 清理卡惠
//		clearBankCard();
		// 清理活动
		clearActivity();
		// 清理团购
//		clearTuan();

		// init BaseBank 重建银行卡库
//		createBank();

	}

	/**
	 * 清除所有数据
	 */
	@Test
	public void initAll() {
		// del data ---------------mongoDB----------
		Set<String> collectionSet = mongoTemplate.getDb().getCollectionNames();
		for (String string : collectionSet) {
			boolean condition = string.equalsIgnoreCase("baseCity") || string.equalsIgnoreCase("baseBank")
					|| string.equalsIgnoreCase("baseShop");
			if (condition) {
				continue;
			} else {
				// mongoTemplate.remove(new Query(), string);
			}
		}

		// del data ------------------redis -------------
		// redisService.getJedis().flushDB();

	}

	/**
	 * 将redis未匹配队列数据score该为0，获取立即匹配资格
	 */
	@Test
	public void resetRedis() {
		// matchRDao.resetUmMapList(DiscountType.Card);
	}

	private void clearTuan() {
		// 删除mongo数据
		mongoTemplate.remove(new Query(), "bizTuan");
		mongoTemplate.remove(new Query(), "bizTuanShop");
		// 删除redis对应数据
		redisService.getJedis().del(new String[] { "match:un:tuan", "dep:tuan:que", "dep:tuans:que" });

		Set<String> tuanSet = redisService.getJedis().keys("*T800*");
		for (String string : tuanSet) {
			redisService.getJedis().del(string);
		}
	}

	private void clearActivity() {
		// del mongo
		mongoTemplate.remove(new Query(), "bizActivity");
		mongoTemplate.remove(new Query(), "bizActivityShop");
		// del redis
		redisService.getJedis().del(new String[] { "match:un:act", "dep:act:que", "dep:actS:que" });
	}

	private void clearBankCard() {
		// del mongo
//		mongoTemplate.remove(new Query(), "bizCard");
		// del redis
		redisService.getJedis().del(new String[] { "match:un:card", "dep:bnk:que" });

		Set<String> bankSet = redisService.getJedis().keys("*bankCard*");
		for (String string : bankSet) {
			redisService.getJedis().del(string);
		}

	}

	/**
	 * 建立银行库
	 */
	private void createBank() {
		Map<Long, String> bankMap = new HashMap<Long, String>();

		bankMap = wrapperBankMap(bankMap);

		for (Entry<Long, String> entry : bankMap.entrySet()) {

			BaseBankDomain bank = new BaseBankDomain();
			bank.setId(entry.getKey());
			bank.setName(entry.getValue());
			baseBankService.save(bank);
		}
	}

	private Map<Long, String> wrapperBankMap(Map<Long, String> bankMap) {
		bankMap.put(1l, "招商银行");
		bankMap.put(2l, "广发银行");
		bankMap.put(3l, "光大銀行");
		bankMap.put(4l, "华夏银行");
		bankMap.put(5l, "建设银行");
		bankMap.put(6l, "民生银行");
		bankMap.put(7l, "农业银行");
		bankMap.put(8l, "浦发银行");
		bankMap.put(9l, "兴业银行");
		bankMap.put(10l, "中国银行");
		bankMap.put(11l, "中信银行");
		bankMap.put(12l, "工商银行");
		bankMap.put(13l, "交通银行");
		bankMap.put(14l, "邮储银行");
		bankMap.put(15l, "平安银行");
		bankMap.put(16l, "深发银行");
		bankMap.put(17l, "宁波银行");
		bankMap.put(18l, "北京银行");
		bankMap.put(19l, "东亚银行");
		bankMap.put(20l, "河北银行");
		bankMap.put(21l, "大连银行");
		bankMap.put(22l, "徽商银行");
		bankMap.put(23l, "温州银行");
		bankMap.put(24l, "重庆农商");
		bankMap.put(25l, "重庆银行");
		bankMap.put(26l, "广州银行");
		bankMap.put(27l, "兰州银行");
		bankMap.put(31l, "鄞州银行");
		bankMap.put(33l, "上海银行");
		bankMap.put(34l, "宁夏银行");
		bankMap.put(35l, "长沙银行");
		bankMap.put(36l, "江苏农信");
		bankMap.put(37l, "福建农信");
		bankMap.put(38l, "成都农商");
		bankMap.put(39l, "富滇银行");
		bankMap.put(40l, "杭州银行");
		return bankMap;
	}
}
